package org.walkerljl.boss.service.monitor.collect.check.support;

import org.springframework.stereotype.Component;
import org.walkerljl.boss.domain.monitor.MonitorResult;
import org.walkerljl.boss.enums.monitor.MonitorObjectType;
import org.walkerljl.boss.service.monitor.collect.task.MonitorTask;
import org.walkerljl.boss.support.enums.Status;
import org.walkerljl.commons.util.HttpUtils;

import java.util.Date;

/**
 * HttpMonitor
 *
 * @author lijunlin
 */
@Component("httpChecker")
public class HttpChecker extends AbstractChecker {

    @Override
    public MonitorResult check0(MonitorTask monitorTask) {
        String responseString = null;
        int code = MonitorResult.CODE_SUCCESS;
        try {
            responseString = new HttpUtils(monitorTask.getTarget()).execute();
        } catch (Throwable e) {
            code = MonitorResult.CODE_FAILURE;
            responseString = e.getMessage();
        }
        MonitorResult result = new MonitorResult();
        result.setAppId(monitorTask.getAppId());
        result.setMonitorObjectId(monitorTask.getMonitorObjectId());
        result.setMonitorType(monitorTask.getType().getValue());
        result.setCode(code);
        result.setContent(responseString);
        result.setStatus(Status.ENABLED.getValue());
        result.setCreated(new Date());
        result.setCreator("");
        result.setModified(result.getCreated());
        result.setModifier(result.getCreator());
        return result;
    }

    @Override
    public MonitorObjectType getType() {
        return MonitorObjectType.HTTP;
    }
}
